package com.yxinsur.imc.web.controller.tool;

import com.alibaba.fastjson.JSON;
import com.yxinsur.imc.business.service.BasicService;
import com.yxinsur.imc.business.drools.model.Message;

import com.yxinsur.imc.common.rocketMq.entity.PantheonChannel;
import com.yxinsur.imc.business.vo.PantheonVo;
import com.yxinsur.imc.common.rocketMq.entity.PantheonChannelOrganization;
import com.yxinsur.imc.common.rocketMq.utils.ProducerUtil;
import org.drools.core.impl.InternalKnowledgeBase;
import org.drools.core.impl.KnowledgeBaseFactory;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collection;

@RequestMapping("/test")
@Controller
public class TestController {
    @Autowired
    private KieBase kieBase;
    @Autowired
    private BasicService basicService;
    @Autowired
    private ProducerUtil producer;

    @ResponseBody
    @RequestMapping("/message")
    public void test(){
        //远程加载规则包

        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        /*kbuilder.add(ResourceFactory.newUrlResource("http://apppackages.oss-cn-beijing.aliyuncs.com/Message.drl?OSSAccessKeyId=LTAI4G19dYs5fMTtr2HsMYmm&Expires=1620802542&Signature=ZfhoMOCRe%2FVgGBvCYlpIviR9Rt8%3D"),
                ResourceType.DRL);*/

        //创建知识库
        InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
        //将规则包添加到知识库中
        knowledgeBase.addPackages(kbuilder.getKnowledgePackages());
        //获得kieSession

        KieSession kieSession = knowledgeBase.newKieSession();
        //KieSession kieSession = kieBase.newKieSession();

        //创建Fact对象
        Message message = new Message();
        message.setMsg("Hello World");
        message.setStatus(0);

        kieSession.insert(message);

        int ruleFiredCount = kieSession.fireAllRules();
        System.out.println("触发了" + ruleFiredCount + "条规则");

        Collection<FactHandle> factHandles = kieSession.getFactHandles();
        for (FactHandle factHandle : factHandles) {
            Message message1 = (Message) kieSession.getObject(factHandle);
            System.out.println(message1.getMsg());
            System.out.println(message1.getStatus());
        }
        kieSession.dispose();
    }

    @ResponseBody
    @RequestMapping("/testBasic")
    public void testBasic(){
        ArrayList<Long> longs = new ArrayList<>();
        longs.add(10L);
        longs.add(26L);
        //BasicFACT fact = basicService.createFact(10L,"2021-05",1L);
        //basicService.calculateCommission(fact,"");
        //System.out.println(fact.toString());
    }

    @ResponseBody
    @RequestMapping("/testMessage")
    public void testMessage(){
        PantheonChannel pantheonChannel = new PantheonChannel();
        pantheonChannel.setChannelName("霍格沃兹大学");
        pantheonChannel.setChannelSimpleName("格兰芬多");
        pantheonChannel.setChannelNo("halibote");
        pantheonChannel.setChannelSource("2");
        ArrayList<PantheonChannel> pantheonChannels = new ArrayList<>();
        pantheonChannels.add(pantheonChannel);
        PantheonVo pantheonVo = new PantheonVo();
        pantheonVo.setType("delete");
        pantheonVo.setDatas(JSON.parseArray(JSON.toJSONString(pantheonChannels)));


        //同步发送消息
        producer.sendMsg("CHANNEL_MSG",JSON.toJSONString(pantheonVo).getBytes(),"messageId");

        PantheonChannelOrganization pantheonChannelOrganization = new PantheonChannelOrganization();
        pantheonChannelOrganization.setOrgName("葛莱芬多");
        pantheonChannelOrganization.setOrgNo("glfdyyds");
        pantheonChannelOrganization.setParentOrgNo("SCDC-CD");
        pantheonChannelOrganization.setOrglSource("2");
        ArrayList<PantheonChannelOrganization> organizations = new ArrayList<>();
        organizations.add(pantheonChannelOrganization);
        PantheonVo pantheonVo1 = new PantheonVo();
        pantheonVo1.setType("delete");
        pantheonVo1.setDatas(JSON.parseArray(JSON.toJSONString(organizations)));
        producer.sendMsg("ORG_MSG",JSON.toJSONString(pantheonVo1).getBytes(),"messageId");
        //单向消息
        //producer.sendOneWayMsg("userMessage", "单向消息".getBytes(), "messageId");
        //异步消息
       // producer.sendAsyncMsg("userMessage", "异步消息".getBytes(), "messageId");
    }
}
